/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-09-03
 * Time: 21:29
 */
class Solution2 {
    public String longestPalindrome(String s) {
        int n = s.length();

        int begin = 0,len = 0;
        for(int i = 0; i < n; i++) { //固定中心点
            // 扩展奇数点
            int left = i,right = i;
            while(left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
                left--;
                right++;
            }
            if(right - left - 1 > len) {
                begin = left+1;
                len = right - left - 1;
            }
            // 扩展偶数点
            left = i;right = i+1;
            while(left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
                left--;
                right++;
            }
            if(right - left - 1 > len) {
                begin = left+1;
                len = right - left - 1;
            }
        }
        return s.substring(begin,begin+len);
    }
}
